package com.apes.scm.masterdata.finance.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.finance.model.PaymentMethodType;
import com.apes.scm.masterdata.finance.repository.PaymentMethodRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 收款方式
 *
 * @author wuq
 * @create 2018-03-22 15:05
 */
@Service("paymentMethodService")
public class PaymentMethodService extends DomainService{

    @Autowired
    PaymentMethodRepository paymentMethodRepository;

    public PaymentMethodType save(SimpleRequest request){
        PaymentMethodType paymentMethodType = request.getO(PaymentMethodType.class);
        paymentMethodType.setCreator(request.getJO().getJSONObject("creator").getString("id"));
        return paymentMethodRepository.saveAndFlush(paymentMethodType);
    }

    public void checkData(SimpleRequest request) {
        String id = request.get("id");
        int version = request.get("version");

        // 从数据库中取数，检查数据
        PaymentMethodType oldPaymentMethodType = paymentMethodRepository.findOne(id);
        if (oldPaymentMethodType == null) throw new RuntimeException("收款方式【" + id + "】在系统中不存在。");
        if (oldPaymentMethodType.getVersion() != version) throw new RuntimeException("记录已更新或已删除，请重新提交！");
    }

    public PaymentMethodType update(SimpleRequest request){
        checkData(request);
        PaymentMethodType paymentMethodType = request.getO(PaymentMethodType.class);
        return paymentMethodRepository.saveAndFlush(paymentMethodType);
    }

    public String delete(SimpleRequest request){
        checkData(request);
        PaymentMethodType paymentMethodType = request.getO(PaymentMethodType.class);
        paymentMethodRepository.delete(paymentMethodType);
        return "删除成功";
    }

    public Page<PaymentMethodType> findAllForPurchasePayment(SimpleRequest request){
        // 使用分页方式
        return paymentMethodRepository.findAllForPurchasePayment(PageRequest.of(request.get("page"), request.get("size")));
    }
    public Page<PaymentMethodType> findAllForUse(SimpleRequest request){
        // 使用分页方式
        return paymentMethodRepository.findAllForUse(PageRequest.of(request.get("page"), request.get("size")));
    }

    public Page<PaymentMethodType> findAllForPay(SimpleRequest request){
        // 使用分页方式
        return paymentMethodRepository.findAllForPay(PageRequest.of(request.get("page"), request.get("size")));
    }

    public Page<PaymentMethodType> findAllPay(SimpleRequest request){
        // 使用分页方式
        return paymentMethodRepository.findAllPay(PageRequest.of(request.get("page"), request.get("size")));
    }

    public Page<PaymentMethodType> findSomePay(SimpleRequest request){
        // 使用分页方式
        return paymentMethodRepository.findSomePay(PageRequest.of(request.get("page"), request.get("size")));
    }

    public Page<PaymentMethodType> findAllPayByRetailValidIsTrue(SimpleRequest request){
        return paymentMethodRepository.findAllPayByRetailValidIsTrue(request.get("key"),PageRequest.of(request.get("page"), request.get("size")));
    }
}
